package com.hippo.core.ui.compose.control

import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hippo.core.R
import com.hippo.core.config.AppStoreKey
import com.hippo.core.core.manager.AppStore
import com.hippo.core.ui.components.ItemText
import com.hippo.core.ui.components.CommonDialog
import com.hippo.core.ui.components.ThemeButton

@Composable
fun TimeFormatDialog(onDismissRequest: () -> Unit,onSave: (String) -> Unit) {

    var timeFormat by remember { mutableStateOf(12) }
    var format by remember { mutableStateOf("") }

    LaunchedEffect(Unit) {
        timeFormat = AppStore.readInt(AppStoreKey.TimeFormat, 12)
        format = AppStore.readString(AppStoreKey.TimeFormatStr,"12-hour format")?:"12-hour format"
    }

    CommonDialog(
        title = stringResource(R.string.setting_25),
        onDismissRequest = onDismissRequest,
    ) {

        ItemText(stringResource(R.string.setting_40), timeFormat == 12,isCenter = true) {
            timeFormat = 12
            format = it
        }

        ItemText(stringResource(R.string.setting_41), timeFormat == 24,isCenter = true) {
            timeFormat =  24
            format = it
        }

        Spacer(modifier = Modifier.height(26.dp))

        ThemeButton(text = stringResource(R.string.save)) {
            AppStore.save(AppStoreKey.TimeFormat, timeFormat)
            AppStore.save(AppStoreKey.TimeFormatStr, format)
            onSave(format)
            onDismissRequest()
        }


    }
}

@Preview
@Composable
private fun TempUnitDialogPreview() {
    TimeFormatDialog(
        onDismissRequest = {},
        onSave = {}
    )
}